#include "BlankForm.hpp"
#include "ui_BlankForm.h"
#include "ExamWindow.hpp"
#include <QSplitter>
#include <QBoxLayout>

BlankForm::BlankForm(RandomQuestion *rd, int type_, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::BlankForm),randomq(rd),type(type_)
{
    ui->setupUi(this);
    QSplitter *splitter=new QSplitter(Qt::Vertical,this);
    splitter->addWidget(ui->plainTextEdit);
    splitter->addWidget(ui->label);
    QVBoxLayout *layout=new QVBoxLayout(this);
    layout->addWidget(splitter);
    setLayout(layout);
    ui->plainTextEdit->setReadOnly(true);
    ui->label->setReadOnly(true);
    status=false;
}

bool BlankForm::checkCorrection(QString *msg)
{
    if(msg){
        *msg=QString::fromUtf8("\u8BF7\u5BF9\u7167\u7B54\u6848");
    }
    if (answer){
        ui->label->setPlainText(*answer);
    }
    status=false;
    return true;
}

BlankForm::~BlankForm()
{
    delete ui;
}

bool BlankForm::nextQuestion(QString *msg)
{
    StaticBlankQuestion *q;
    if (type==0){
        q=randomq->randomChooseBlankQuestion();
    }else if(type==1){
        q=randomq->randomChallengeQuestion();
    }else{
        q=randomq->randomShortAnswerQuestion();
    }
    if (!q){
        if (msg){
            *msg=QString::fromUtf8("\u586B\u7A7A\u7B80\u7B54\u9898\u5DF2\u5168\u90E8"
                                   "\u7528\u5B8C\uFF0C\u8BF7\u6DFB\u52A0\u65B0\u9898\u5E93");
        }
        return status=false;
    }
    ui->plainTextEdit->setPlainText(q->content);
    ui->label->clear();
    answer=&q->answer;
    if (msg){
        *msg=QString::fromUtf8("\u9898\u76EEID\uFF1A%1").arg(q->id);
    }
    return status=true;
}

void BlankForm::updateConfig(const ExamConfig *cfg)
{
    ui->plainTextEdit->setFont(cfg->mainfont);
    ui->label->setFont(cfg->mainfont);
}

void BlankForm::clearText()
{
    if (!status){
        ui->plainTextEdit->clear();
        ui->label->clear();
    }
}
